import os.path

import yaml


def get_project_path():
    path = os.path.dirname(__file__).split("utils")[0]
    return path


def read_config_yaml(var_f_name,var_t_name):
    with open(os.path.join(get_project_path(), 'config.yaml'), 'r', encoding='utf-8') as f:
        data = yaml.load(f.read(), Loader=yaml.FullLoader)
        return data[var_f_name][var_t_name]


def read_testcase_yaml(filename):
    with open(os.path.join(get_project_path(), "testcases", filename), 'r', encoding='utf-8') as f:
        data = yaml.load(f.read(), Loader=yaml.FullLoader)
        return data


def write_extract_yaml(extract_dict):
    with open(str(get_project_path()) + "extract.yaml", 'a+', encoding='utf-8') as f:
        yaml.dump(extract_dict, f)


def read_extract_yaml():
    with open(os.path.join(get_project_path(), 'extract.yaml'), 'r', encoding='utf-8') as f:
        data = yaml.load(f.read(), Loader=yaml.FullLoader)
        return data

def clear_extract_yaml():
    with open(os.path.join(get_project_path(), 'extract.yaml'), 'w') as f:
        f.truncate()
